<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Home extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        
        $this->load->model( 'seo_model' );
        $this->load->model( 'setting_model' );
        $this->load->model( 'links_model' );
        $this->load->model( 'category_model' );
        $this->load->model( 'content_model' );
        $this->load->library( 'Location', array (
                'title' => $this->lang->line( 'page_index' ) 
        ) );
        
        // 设置
        $this->load->vars( $this->setting_model->get() );
        
        // SEO
        $seo_uri = $this->uri->uri_string() . $this->config->item( 'url_suffix' );
        if ($seo = $this->seo_model->get( $seo_uri ))
        {
            $this->load->vars( $seo );
        }
        
        // 导航
        $nav = $this->links_model->get( 0 );
        $this->load->vars( 'nav', $nav );
        $sub_ids = array ();
        foreach ( $nav as $i )
        {
            if (! empty( $i['sub_id'] ))
            {
                $sub_ids[] = $i['sub_id'];
            }
        }
        $sub_list = $this->category_model->get_list_by_ids( $sub_ids );
        $ids[0] = $ids[1] = array ();
        foreach ( $sub_list as $sub )
        {
            if (empty( $sub['model'] ))
            {
                $ids[0][] = $sub['id'];
            }
            else
            {
                $ids[1][] = $sub['id'];
            }
        }
        $this->load->vars( 'sub_menus_content', $this->content_model->get_sub_menus( $ids[0] ) );
        $this->load->vars( 'sub_menus_category', $this->category_model->get_sub_menus( $ids[1] ) );
        
        // 焦点图
        if ($this->request === 'home/index')
        {
            $this->load->vars( 'banner_data', $this->links_model->get( 1 ) );
        }
        else
        {
            $this->load->vars( 'banner_data', $this->links_model->get( 2 ) );
        }
        
        // 在线客服
        $this->load->vars( 'kf_list', $this->setting_model->get( 'kf_enable' ) ? $this->links_model->get( 4 ) : null );
    }
    function index()
    {
        // 缓存页面
        if ($this->config->item( 'page_cache' ) && $this->setting_model->get( 'page_cache' ))
        {
            $this->output->cache( $this->setting_model->get( 'cache_time' ) );
        }        
        
        // 文章介绍菜单：分类名称作为菜单标题，该分类下所有文章作为菜单项
        $this->category_model->load( 1 );
        $this->load->vars( 'category', $this->category_model->get_data() );
        $category = $this->category_model->get_current( 0 );
        $this->load->vars( 'menu', $this->content_model->get_list( $category['id'], 1 ) );
        
        // 推荐新闻
        $this->load->vars( 'flag_news', $this->content_model->get_flag_list( 1, 10, 6 ) );
        
        // 推荐产品
        $this->load->vars( 'flag_product', $this->content_model->get_flag_list( 1, 12, 8 ) );
        
        // 友情链接
        $this->load->vars( 'links', $this->links_model->get( 3 ) );
        
        $this->load->view( 'master:index' );
    }
    function info()
    {
        $this->_info();
    }
    function news_list()
    {
        $this->_list( 2, 'master:news_list' );
    }
    function news()
    {
        $this->_content( 2, 'master:news' );
    }
    function product_list()
    {
        $this->_list( 3, 'master:product_list' );
    }
    function product()
    {
        $this->_content( 3, 'master:product' );
    }
    function download_list()
    {
        $this->_list( 4, 'master:download_list' );
    }
    function download()
    {
        $this->_content( 4, 'master:download' );
    }
    function guestbook()
    {
        // 文章介绍菜单：分类名称作为菜单标题，该分类下所有文章作为菜单项
        $this->category_model->load( 1 );
        $this->load->vars( 'category', $this->category_model->get_data() );
        $category = $this->category_model->get_current( 0 );
        $this->load->vars( 'menu', $this->content_model->get_list( $category['id'], 1 ) );
        
        // 位置
        $this->location->add( $this->lang->line( 'page_feedback' ), '' );
        $this->load->vars( 'location', $this->location->get_data() );
        
        // 缓存页面
        if ($this->config->item( 'page_cache' ) && $this->setting_model->get( 'page_cache' ))
        {
            $this->output->cache( $this->setting_model->get( 'cache_time' ) );
        }
        
        $this->load->view( 'master:guestbook' );
    }
    function search()
    {
        $gid = 3;
        
        // 左侧菜单
        $this->category_model->load( $gid );
        $this->load->vars( 'category', $this->category_model->get_data() );
        $this->load->vars( 'menu_selected', 0 );
        
        // 位置
        $this->location->add( $this->lang->line( 'page_product_search' ), '' );
        $this->load->vars( 'location', $this->location->get_data() );
        
        // 关键字
        $k = $this->input->get( 'k' );
        $k = trim( substr_utf8( $k, 10, 0, '' ) );
        if (empty( $k ))
        {
            $this->load->vars( 'pager', '' );
            $this->load->vars( 'list', array () );
            $this->load->view( 'master:product_list' );
            return;
        }
        
        // 分页
        $this->load->library( 'pagination' );
        $pager_config['total_rows'] = $this->content_model->search_count( $k, $gid );
        $pager_config['per_page'] = 20;
        $pager_config['uri_segment'] = 3;
        $pager_config['base_url'] = site_url( $this->request );
        $pager_config['reuse_query_string'] = true;
        $this->pagination->initialize( $pager_config );
        $this->load->vars( 'pager', $this->pagination->create_links() );
        
        // 列表
        $begin = ( int ) $this->uri->segment( $pager_config['uri_segment'], 0 );
        $begin < 0 && $begin = 0;
        $list = $this->content_model->search( $k, $gid, $pager_config['per_page'], $begin );
        $this->load->vars( 'list', $list );
        
        $this->load->view( 'master:product_list' );
    }
    function page()
    {
        $this->load->model( 'page_model' );
        $id = ( int ) $this->uri->segment( 3 );
        if (($page = $this->page_model->get( $id )) && ($tpl = $this->page_model->get_tpl( $page['tpl_id'] )))
        {
            $tpl_var = json_decode( $tpl['tpl_var'], true );
            if (! is_array( $tpl_var ))
            {
                $tpl_var = array ();
            }
            extract( $tpl_var );
            $tpl_content = isset( $tpl['tpl_content'] ) ? $tpl['tpl_content'] : '';
            // 首尾添加php脚本标签，使所有原样输出的html文本处于php脚本标签的中间
            $tpl_content = "<?php ?>{$tpl_content}<?php ?>";
            // 用echo语句替换原样输出的html文本
            $tpl_content = preg_replace_callback( '/\?>([\s\S]+?)<\?/', function ($m)
            {
                return '?><?php echo "' . addcslashes( $m[1], "\r\n\\\"" ) . '";?><?';
            }, $tpl_content );
            // 去掉php脚本标签并执行标签里面的代码
            eval( str_replace( array (
                    '<?php',
                    '?>' 
            ), '', $tpl_content ) );
        }
        else
        {
            show_404();
        }
    }
    private function _info($gid = 1, $tpl = null)
    {
        $id = ( int ) $this->uri->segment( 3 );
        if ($content = $this->content_model->get( $id, $gid ))
        {
            $this->load->vars( $content );
            $cid = $content['cid'];
            
            // 左侧菜单
            $this->category_model->load( $gid );
            if (! $this->category_model->exist( $cid ))
            {
                show_404();
            }
            $this->load->vars( 'category', $this->category_model->get_data() );
            $this->load->vars( 'menu', $this->content_model->get_list( $cid, $gid ) );
            $this->load->vars( 'menu_selected', $content['id'] );
            
            // 位置
            foreach ( $this->category_model->get_path( $cid ) as $path )
            {
                $this->location->add( $path[$this->lang->line( 'field_category_title' )], '' );
            }
            $this->location->add( $content[$this->lang->line( 'field_content_title' )], '' );
            $this->load->vars( 'location', $this->location->get_data() );
            
            // 缓存页面
            if ($this->config->item( 'page_cache' ) && $this->setting_model->get( 'page_cache' ))
            {
                $this->output->cache( $this->setting_model->get( 'cache_time' ) );
            }
            
            $this->load->view( empty( $tpl ) ? 'master:info' : $tpl );
        }
        else
        {
            show_404();
        }
    }
    private function _list($gid, $tpl)
    {
        if ($this->category_model->load( $gid ))
        {
            // 左侧菜单
            $this->load->vars( 'category', $this->category_model->get_data() );
            $current = $this->category_model->get_current( ( int ) $this->uri->segment( 3 ) );
            $this->load->vars( 'menu_selected', $current['id'] );
            
            // seo
            $this->load->vars( 'head_title', $current['title'] );
            $this->load->vars( 'head_keywords', $current['title'] );
            $this->load->vars( 'head_description', $current['title'] );
            
            // 位置
            foreach ( $this->category_model->get_path( $current['id'] ) as $path )
            {
                $this->location->add( $path[$this->lang->line( 'field_category_title' )], $path['list_uri'] . '/' . $path['id'] );
            }
            $this->load->vars( 'location', $this->location->get_data() );
            
            // 列表条件
            $cids = $this->category_model->get_ids( $current['id'] );
            
            // 分页
            $this->load->library( 'pagination' );
            $pager_config['total_rows'] = $this->content_model->get_list_count( $cids, $gid );
            $pager_config['per_page'] = 20;
            $pager_config['uri_segment'] = 4;
            $pager_config['base_url'] = site_url( $this->request . '/' . $current['id'] );
            $this->pagination->initialize( $pager_config );
            $this->load->vars( 'pager', $this->pagination->create_links() );
            
            // 列表
            $begin = ( int ) $this->uri->segment( $pager_config['uri_segment'], 0 );
            $begin < 0 && $begin = 0;
            $list = $this->content_model->get_list( $cids, $gid, $pager_config['per_page'], $begin, true );
            $this->load->vars( 'list', $list );
            
            // 缓存页面
            if (! empty( $list ) && $this->config->item( 'page_cache' ) && $this->setting_model->get( 'page_cache' ))
            {
                $this->output->cache( $this->setting_model->get( 'cache_time' ) );
            }
            
            $this->load->view( $tpl );
        }
        else
        {
            show_404();
        }
    }
    private function _content($gid, $tpl)
    {
        $id = ( int ) $this->uri->segment( 3 );
        if ($content = $this->content_model->get( $id, $gid ))
        {
            $this->load->vars( $content );
            $cid = $content['cid'];
            
            // 左侧菜单
            $this->category_model->load( $gid );
            if (! $this->category_model->exist( $cid ))
            {
                show_404();
            }
            $this->load->vars( 'category', $this->category_model->get_data() );
            $this->load->vars( 'menu_selected', $content['cid'] );
            
            // 位置
            foreach ( $this->category_model->get_path( $cid ) as $path )
            {
                $this->location->add( $path[$this->lang->line( 'field_category_title' )], $path['list_uri'] . '/' . $path['id'] );
            }
            $this->load->vars( 'location', $this->location->get_data() );
            
            // 上一条下一条
            $this->load->vars( $this->content_model->get_near( $id, $gid ) );
            
            // 缓存页面
            if ($this->config->item( 'page_cache' ) && $this->setting_model->get( 'page_cache' ))
            {
                $this->output->cache( $this->setting_model->get( 'cache_time' ) );
            }
            
            $this->load->view( $tpl );
        }
        else
        {
            show_404();
        }
    }
    function _remap()
    {
        // 已定义方法直接调用
        $method = $this->router->method;
        if (in_array( strtolower( $method ), array_map( 'strtolower', get_class_methods( $this ) ), true ))
        {
            $this->$method();
            return;
        }
        
        // 通过分类增加的方法
        if ($content_model = $this->category_model->get_content_model( $this->request ))
        {
            switch ($content_model['model'])
            {
                case '0' :
                    $this->_info( $content_model['gid'], $content_model['content_tpl'] );
                    return;
                case '1' :
                    if ($this->request == $content_model['list_uri'])
                    {
                        $this->_list( $content_model['gid'], empty( $content_model['list_tpl'] ) ? 'master:news_list' : $content_model['list_tpl'] );
                    }
                    else
                    {
                        $this->_content( $content_model['gid'], empty( $content_model['content_tpl'] ) ? 'master:news' : $content_model['content_tpl'] );
                    }
                    return;
                case '2' :
                    if ($this->request == $content_model['list_uri'])
                    {
                        $this->_list( $content_model['gid'], empty( $content_model['list_tpl'] ) ? 'master:product_list' : $content_model['list_tpl'] );
                    }
                    else
                    {
                        $this->_product( $content_model['gid'], empty( $content_model['content_tpl'] ) ? 'master:product' : $content_model['content_tpl'] );
                    }
                    return;
                case '3' :
                    if ($this->request == $content_model['list_uri'])
                    {
                        $this->_list( $content_model['gid'], empty( $content_model['list_tpl'] ) ? 'master:download_list' : $content_model['list_tpl'] );
                    }
                    else
                    {
                        $this->_content( $content_model['gid'], empty( $content_model['content_tpl'] ) ? 'master:download' : $content_model['content_tpl'] );
                    }
                    return;
            }
        }
        // 报错
        show_404();
    }
}
